package com.iknow.util;

import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
import com.iknow.IKnow;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

public class SpannableUtil
{
  public static Spannable formatText(String paramString, TextStyle paramTextStyle)
  {
    Object localObject;
    if ((paramString.trim().length() == 0) || (paramTextStyle == null))
      localObject = new SpannableStringBuilder(paramString);
    while (true)
    {
      return localObject;
      SpannableString localSpannableString = new SpannableString(paramString);
      String str = null;
      if (!(StringUtil.isEmpty(paramTextStyle.txtColor)))
        str = paramTextStyle.txtColor.split(":")[1];
      if (paramTextStyle.tf)
        localSpannableString.setSpan(new AbsoluteSizeSpan(20), 0, localSpannableString.length(), 33);
      if (!(StringUtil.isEmpty(str)))
        localSpannableString.setSpan(new ForegroundColorSpan(Color.parseColor(str)), 0, localSpannableString.length(), 33);
      if (paramTextStyle.underLine)
        localSpannableString.setSpan(new UnderlineSpan(), 0, localSpannableString.length(), 33);
      localObject = localSpannableString;
    }
  }

  public static Spannable getHyperLink(String paramString1, String paramString2)
  {
    SpannableString localSpannableString;
    if (StringUtil.isEmpty(paramString2))
    {
      localSpannableString = new SpannableString(paramString1);
      localSpannableString.setSpan(new URLSpan(paramString1), 0, localSpannableString.length(), 33);
    }
    while (true)
    {
      return localSpannableString;
      localSpannableString = new SpannableString(paramString2);
      localSpannableString.setSpan(new URLSpan(paramString1), 0, localSpannableString.length(), 33);
    }
  }

  public static Spannable mergeSpanna(Spannable paramSpannable1, Spannable paramSpannable2)
  {
    String str;
    Object localObject1;
    int k;
    int l;
    int i1;
    if ((paramSpannable1 == null) && (paramSpannable2 == null))
      localObject1 = new SpannableString("");
    while (true)
    {
      while (true)
      {
        while (true)
        {
          return localObject1;
          if (paramSpannable1 != null)
            break;
          localObject1 = paramSpannable2;
        }
        if (paramSpannable2 != null)
          break;
        localObject1 = paramSpannable1;
      }
      str = paramSpannable1.toString() + paramSpannable2.toString();
      if (!(StringUtil.isEmpty(str)))
        break;
      localObject1 = new SpannableString(str);
    }
    Object[] arrayOfObject1 = paramSpannable1.getSpans(0, paramSpannable1.length(), Object.class);
    Object[] arrayOfObject2 = paramSpannable2.getSpans(0, paramSpannable2.length(), Object.class);
    SpannableString localSpannableString = new SpannableString(str);
    int i = arrayOfObject1.length;
    int j = 0;
    if (j >= i)
    {
      k = paramSpannable1.toString().length();
      l = arrayOfObject2.length;
      i1 = 0;
    }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          if (i1 < l)
            break label223;
          localObject1 = localSpannableString;
        }
        Object localObject2 = arrayOfObject1[j];
        localSpannableString.setSpan(localObject2, paramSpannable1.getSpanStart(localObject2), paramSpannable1.getSpanEnd(localObject2), paramSpannable1.getSpanFlags(localObject2));
        ++j;
      }
      label223: Object localObject3 = arrayOfObject2[i1];
      int i2 = paramSpannable2.getSpanStart(localObject3);
      int i3 = paramSpannable2.getSpanEnd(localObject3);
      int i4 = paramSpannable2.getSpanFlags(localObject3);
      localSpannableString.setSpan(localObject3, i2 + k, i3 + k, i4);
      ++i1;
    }
  }

  public static Spannable reflesh(Spannable paramSpannable)
  {
    Object localObject1;
    int i;
    Object[] arrayOfObject;
    SpannableStringBuilder localSpannableStringBuilder;
    int k;
    if ((paramSpannable == null) || (StringUtil.isEmpty(paramSpannable.toString())))
      localObject1 = new SpannableStringBuilder();
    while (true)
    {
      return localObject1;
      i = IKnow.getTextSize();
      arrayOfObject = paramSpannable.getSpans(0, paramSpannable.length(), Object.class);
      localSpannableStringBuilder = new SpannableStringBuilder(paramSpannable.toString());
      localSpannableStringBuilder.setSpan(new AbsoluteSizeSpan(i), 0, localSpannableStringBuilder.length(), 33);
      int j = arrayOfObject.length;
      k = 0;
      if (k < j)
        break;
      localObject1 = localSpannableStringBuilder;
    }
    Object localObject2 = arrayOfObject[k];
    int l = paramSpannable.getSpanStart(localObject2);
    int i1 = paramSpannable.getSpanEnd(localObject2);
    int i2 = paramSpannable.getSpanFlags(localObject2);
    int i3 = i - 16;
    if (localObject2 instanceof AbsoluteSizeSpan)
      localSpannableStringBuilder.setSpan(new AbsoluteSizeSpan(i3 + ((AbsoluteSizeSpan)localObject2).getSize()), l, i1, i2);
    while (true)
    {
      while (true)
        ++k;
      localSpannableStringBuilder.setSpan(localObject2, l, i1, i2);
    }
  }

  public static TextStyle setTextStyle(Map<String, String> paramMap, String paramString)
  {
    String str1;
    Object localObject;
    if (paramString.trim().length() == 0)
    {
      localObject = null;
      return localObject;
    }
    TextStyle localTextStyle = new TextStyle();
    Iterator localIterator = paramMap.keySet().iterator();
    do
    {
      while (!(localIterator.hasNext()))
        localObject = localTextStyle;
      str1 = (String)paramMap.get((String)localIterator.next());
    }
    while (StringUtil.isEmpty(str1));
    StringTokenizer localStringTokenizer = new StringTokenizer(str1, ";", false);
    while (true)
    {
      String str2;
      do
        while (true)
        {
          while (true)
          {
            if (!(localStringTokenizer.hasMoreElements()));
            str2 = localStringTokenizer.nextToken();
            if (!(str2.equals("b:1")))
              break;
            localTextStyle.tf = true;
          }
          if (!(str2.equals("u:1")))
            break;
          localTextStyle.underLine = true;
        }
      while (!(str2.contains("cl:")));
      localTextStyle.txtColor = str2;
    }
  }

  public static Spannable subSpanna(Spannable paramSpannable, int paramInt1, int paramInt2)
  {
    Object localObject1;
    if ((paramSpannable == null) || (paramSpannable.length() == 0))
    {
      localObject1 = paramSpannable;
      return localObject1;
    }
    if ((paramInt1 < 0) || (paramInt2 > paramSpannable.length()))
      throw new ArrayIndexOutOfBoundsException();
    SpannableString localSpannableString = new SpannableString(paramSpannable.toString().substring(paramInt1, paramInt2));
    Object[] arrayOfObject = paramSpannable.getSpans(0, paramSpannable.length(), Object.class);
    int i = localSpannableString.length();
    int j = arrayOfObject.length;
    int k = 0;
    while (true)
    {
      while (k >= j)
        localObject1 = localSpannableString;
      Object localObject2 = arrayOfObject[k];
      int l = paramSpannable.getSpanStart(localObject2) - paramInt1;
      int i1 = paramSpannable.getSpanEnd(localObject2) - paramInt1;
      int i2 = paramSpannable.getSpanFlags(localObject2);
      if (l < 0)
        l = 0;
      if (i1 > i)
        i1 = i;
      if ((l <= i) && (i1 > 0))
        localSpannableString.setSpan(localObject2, l, i1, i2);
      ++k;
    }
  }

  public static class TextStyle
  {
    public boolean tf = false;
    public String txtColor;
    public boolean underLine = false;

    public TextStyle()
    {
      this.txtColor = null;
    }
  }
}